<?php
namespace App\Task;

use Hyperf\DbConnection\Db;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Utils\ApplicationContext;
use Psr\Log\LoggerInterface;


class DyGetUserViewRecordsTask
{
    protected LoggerInterface $logger;

    public function __construct(protected LoggerFactory $loggerFactory)
    {
        $this->logger = $this->loggerFactory->get('log', 'DyGetUserViewRecords');
    }

    public function execute()
    {
        $container = ApplicationContext::getContainer();
        $defalutRedis = $container->get(\Hyperf\Redis\RedisFactory::class)->get('default');
        $redis = $container->get(\Hyperf\Redis\RedisFactory::class)->get('dy');

        try {
            $this->logger->info(date('Y-m-d H:i:s') . '------------------------获取抖音用户的观看记录');


            $month =  date('Ym',time());
            $monthLogKey = 'user_view_month_table'.$month;
            //判断是否有创建当月的数据库
            $monthVal = $defalutRedis->get($monthLogKey);

            if(empty($monthVal)){
                $this->logger->info(date('Y-m-d H:i:s') . '------------------------没有创建对应的月份表，请稍后重试');
            }

            $userviewKey =  'records_dy';
            $this->logger->info('剩余的数据总量：',[$redis->llen($userviewKey)]);
            $insertAllData = array();
            for ($i=0;$i<3000;$i++){

                $userviewDatum = $redis->lPop($userviewKey);
                if(!empty($userviewDatum)){
                    $viewDatum = json_decode($userviewDatum,true);
                    if(!empty($viewDatum)){
                        $insertData = [];
                        $insertData['applet_id'] = $viewDatum['applet_id'] ?? '';
                        $insertData['user_id'] = $viewDatum['user_id'] ?? '';
                        $insertData['playlet_id'] = $viewDatum['playlet_id'] ?? '';
                        $insertData['playlet_number'] = $viewDatum['playlet_number'] ?? '';
                        if(!empty($viewDatum['create_time'])){
                            $insertData['view_at'] = date('Y-m-d H:i:s',$viewDatum['create_time']);
                        }
                        $insertData['redis_value'] = $userviewDatum;
                        $insertData['created_at'] = date('Y-m-d H:i:s',time());
                        $insertData['updated_at'] = date('Y-m-d H:i:s',time());
                        $insertAllData[] = $insertData;
                    }else{
                        $this->logger->info('获取的数据有问题：',[$userviewDatum]);
                    }
                }else{
                    $this->logger->info('没有数据');
                    break;
                }
            }
            if(!empty($insertAllData)){
                $chunkData = array_chunk($insertAllData,2000);
                foreach ($chunkData as $key  => $val){
                    $this->logger->info('插入数据',[count($val)]);
                    Db::table('data_user_viewing_log_'.$month)->insert($val);
                }
            }

        }catch (\Exception $e){
            $this->logger->error('请求|错误：' . $e->getLine() . '|' . $e->getMessage());
        }
        $this->logger->info(date('Y-m-d H:i:s') . '------------------------结束定时抖音用户的观看记录');
    }
}